﻿using CommonTool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tool;
using System.Drawing;
using System.Diagnostics;
using Motions;
using AutoMachine;
using Station;
using System.Windows.Forms.DataVisualization.Charting;

namespace Motions
{
    /// <summary>
    /// 上下料弹夹
    /// </summary>
    class UpAndDownLoad : StationBase
    {
        #region CT计算
        private Stopwatch CTtime = new Stopwatch();

        public string CT = "";

        #endregion

        #region 流程步

        private int AutoStep = 0;
        private int RunTimes = 1;
        private bool ChaiFenFlag = true;
        #endregion

        public UpAndDownLoad(string name) : base(name) { }

        /// <summary>
        /// 停止流程
        /// </summary>
        public override void StopWork()
        {
            this.AutoStep = 0;
            RunTimes = 1;
            ChaiFenFlag = true;
            SystemMgr.GetInstance().WriteSysInt(SysInt.UpAndDownLoadStep, 0);
        }

        /// <summary>
        /// 急停流程
        /// </summary>
        public override void EmgWork()
        {
            this.AutoStep = 0;
            RunTimes = 1;
            ChaiFenFlag = true;
            SystemMgr.GetInstance().WriteSysInt(SysInt.UpAndDownLoadStep, 0);

        }
        ModeBusSend modeBusSend = new ModeBusSend();

        public override void AutoWork()
        {

            WriteSysInt(SysInt.UpAndDownLoadStep, AutoStep);
            WaitRun();
            switch (AutoStep)
            {
                case 0: 
                    GlobalVar.CCDTCP.Send(modeBusSend.SetD05Off);//旋转气缸原位关闭
                    GlobalVar.CCDTCP.Send(modeBusSend.SetD06On);//旋转气缸动位打开
                    StartTime = DateTime.Now.Ticks;
                    Thread.Sleep(100);                  
                    AutoStep = 10;
                    break;
                case 10:
                    {
                        if (MotionMgr.GetInstance().ReadIn("旋转气缸动点"))
                        {
                            LogView.GetInstance().ShowLog(LogView.LogType.Oper, "旋转气缸到达动点");

                            AutoStep = 100;
                            Thread.Sleep(int.Parse(SystemCfg.Param["DownWaiteTime"].Value.ToString()));//测量前等待时间
                        }
                        else if (DateTime.Now.Ticks - StartTime > 3000 * 10000)
                        {
                            if (PausedAlarm("旋转气缸动点异常,请检查异常"))
                            {
                                AutoStep = 100;
                            }
                            else
                            {
                                AutoStep = 0;
                            }
                        }
                    }

                    break;
                case 100:
                    {
                        GlobalVar.CCDTCP.Send(modeBusSend.SetD07Off);
                        GlobalVar.CCDTCP.Send(modeBusSend.SetD08On);
                        StartTime = DateTime.Now.Ticks;
                        Thread.Sleep(100);
                        AutoStep = 200;
                        //气缸下降
                    }
                    break;

                case 200:
                    {
                        if (MotionMgr.GetInstance().ReadIn("下压气缸动点"))
                        {
                            LogView.GetInstance().ShowLog(LogView.LogType.Oper, "下压气缸到达动点");

                            AutoStep = 300;
                            Thread.Sleep(int.Parse(SystemCfg.Param["DownWaiteTime"].Value.ToString()));//测量前等待时间
                        }
                        else if (DateTime.Now.Ticks - StartTime > 3000 * 10000)
                        {
                            if (PausedAlarm("下压气缸动点异常,请检查异常"))
                            {
                                AutoStep = 300;
                            }
                            else
                            {
                                AutoStep = 100;
                            }
                        }
                        //气缸下降到位
                    }
                    break;

                case 300:
                    {
                        GlobalVar.LCRTCP.Send("FETCH?\r\n");
                        LogView.GetInstance().ShowLog(LogView.LogType.Oper, "获取仪器测量值");
                        TestBool = false;
                        AutoStep = 400;
                        //获取测量值
                    }
                    break;

                case 400:
                    {
                        if (TestBool)
                        {
                            AutoStep = 500;
                        }
                        //测量结束
                    }
                    break;

                case 500:
                    {
                        GlobalVar.CCDTCP.Send(modeBusSend.SetD08Off);
                        GlobalVar.CCDTCP.Send(modeBusSend.SetD07On);
                        LogView.GetInstance().ShowLog(LogView.LogType.Oper, "下压气缸原点");
                        StartTime = DateTime.Now.Ticks;
                        AutoStep = 600;
                        //气缸上升
                    }
                    break;
                case 600:
                    {
                        if (MotionMgr.GetInstance().ReadIn("下压气缸原点"))
                        {
                            AutoStep = 700;
                        }
                        else if (DateTime.Now.Ticks - StartTime > 3000 * 10000)
                        {
                            if (PausedAlarm("下压气缸原点异常,请检查异常"))
                            {
                                AutoStep = 700;
                            }
                            else
                            {
                                AutoStep = 500;
                            }

                        }

                    }
                    break;
                case 5500: //NG下料移栽动位
                    {
                        GlobalVar.CCDTCP2.Send(modeBusSend.SetD01Off);//移栽气缸原位关闭
                        GlobalVar.CCDTCP2.Send(modeBusSend.SetD02On);//移栽气缸动位打开
                        LogView.GetInstance().ShowLog(LogView.LogType.Oper, "移栽气缸动位");
                        StartTime = DateTime.Now.Ticks;
                        AutoStep = 5600;                       
                    }
                    break;
                case 5600:
                    {
                        if (MotionMgr.GetInstance().ReadIn("移栽气缸动点"))
                        {
                            AutoStep = 5700;
                        }
                        else if (DateTime.Now.Ticks - StartTime > 3000 * 10000)
                        {
                            if (PausedAlarm("移栽气缸动位异常,请检查异常"))
                            {
                                AutoStep = 5700;
                            }
                            else
                            {
                                AutoStep = 5500;
                            }

                        }

                    }
                    break;
                case 5700: //NG下料吸料
                    {
                        GlobalVar.CCDTCP2.Send(modeBusSend.SetD03Off);//吸料气缸原位关闭
                        GlobalVar.CCDTCP2.Send(modeBusSend.SetD04On);//吸料气缸动位打开
                        LogView.GetInstance().ShowLog(LogView.LogType.Oper, "吸料气缸动位");
                        StartTime = DateTime.Now.Ticks;
                        AutoStep = 5800;                       
                    }
                    break;
                case 5800:
                    {
                        if (MotionMgr.GetInstance().ReadIn("吸料气缸动点"))
                        {
                            AutoStep = 5700;
                        }
                        else if (DateTime.Now.Ticks - StartTime > 3000 * 10000)
                        {
                            if (PausedAlarm("吸料气缸动位异常,请检查异常"))
                            {
                                AutoStep = 5700;
                            }
                            else
                            {
                                AutoStep = 5500;
                            }

                        }

                    }
                    break;
                case 700:
                    {
                        LogView.GetInstance().ShowLog(LogView.LogType.Oper, "测量结束");
                        Pause();
                        AutoStep = 0;
                    }
                    break;
            }
        }

        ///// <summary>
        ///// 自动流程
        ///// </summary>

        //public override void AutoWorkXX()
        //{

        //    WriteSysInt(SysInt.UpAndDownLoadStep, AutoStep);
        //    WaitRun();
        //    switch (AutoStep)
        //    {
        //        case 0:
        //            GlobalVar.CCDTCP.Send(modeBusSend.SetD04On);//打开真空
        //            StartTime = DateTime.Now.Ticks;
        //            AutoStep = 10;
        //            break;
        //        case 10:
        //            {
        //                if (MotionMgr.GetInstance().ReadIn("真空信号"))
        //                {
        //                    LogView.GetInstance().ShowLog(LogView.LogType.Oper, "打开真空");

        //                    AutoStep = 100;
        //                }
        //                else if (DateTime.Now.Ticks - StartTime > 3000 * 10000)
        //                {
        //                    if (PausedAlarm("吸真空异常,请检查异常"))
        //                    {
        //                        AutoStep = 100;
        //                    }
        //                    else
        //                    {
        //                        AutoStep = 0;
        //                    }
        //                }
        //                //真空满足
        //            }

        //            break;
        //        case 100:
        //            {
        //                GlobalVar.CCDTCP.Send(modeBusSend.SetD05Off);
        //                GlobalVar.CCDTCP.Send(modeBusSend.SetD06On);
        //                StartTime = DateTime.Now.Ticks;
        //                Thread.Sleep(100);
        //                AutoStep = 200;
        //                //气缸下降
        //            }
        //            break;

        //        case 200:
        //            {
        //                if (MotionMgr.GetInstance().ReadIn("气缸原点"))
        //                {
        //                    LogView.GetInstance().ShowLog(LogView.LogType.Oper, "气缸下降");

        //                    AutoStep = 300;
        //                    Thread.Sleep(int.Parse(SystemCfg.Param["DownWaiteTime"].Value.ToString()));//测量前等待时间
        //                }
        //                else if (DateTime.Now.Ticks - StartTime > 3000 * 10000)
        //                {
        //                    if (PausedAlarm("气缸下降异常,请检查异常"))
        //                    {
        //                        AutoStep = 300;
        //                    }
        //                    else
        //                    {
        //                        AutoStep = 100;
        //                    }
        //                }
        //                //气缸下降到位
        //            }
        //            break;

        //        case 300:
        //            {
        //                GlobalVar.LCRTCP.Send("FETCH?\r\n");
        //                LogView.GetInstance().ShowLog(LogView.LogType.Oper, "获取仪器测量值");
        //                TestBool = false;
        //                AutoStep = 400;
        //                //获取测量值
        //            }
        //            break;

        //        case 400:
        //            {
        //                if (TestBool)
        //                {
        //                    AutoStep = 500;
        //                }
        //                //测量结束
        //            }
        //            break;

        //        case 500:
        //            {
        //                GlobalVar.CCDTCP.Send(modeBusSend.SetD06Off);
        //                GlobalVar.CCDTCP.Send(modeBusSend.SetD05On);
        //                LogView.GetInstance().ShowLog(LogView.LogType.Oper, "气缸上升");
        //                StartTime = DateTime.Now.Ticks;
        //                AutoStep = 600;
        //                //气缸上升
        //            }
        //            break;
        //        case 600:
        //            {
        //                if (MotionMgr.GetInstance().ReadIn("气缸动点"))
        //                {
        //                    GlobalVar.CCDTCP.Send(modeBusSend.SetD04Off);


        //                    AutoStep = 700;
        //                }
        //                else if(DateTime.Now.Ticks - StartTime > 3000 * 10000)
        //                {
        //                    if (PausedAlarm("气缸上升异常,请检查异常"))
        //                    {
        //                        AutoStep =700;
        //                    }
        //                    else
        //                    {
        //                        AutoStep = 500;
        //                    }

        //                }

        //            }
        //            break;
        //        case 700:
        //            {
        //                LogView.GetInstance().ShowLog(LogView.LogType.Oper, "测量结束");
        //                Pause();
        //                AutoStep = 0;
        //            }
        //            break;
        //    }
        //}


    }
}
